package com.tiany.kotlin_study

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.tiany.kotlin_study.Class.Fruit
import com.tiany.kotlin_study.MyAdapter.FruitAdapter
import kotlinx.android.synthetic.main.activity_first.*

class FirstActivity : AppCompatActivity() {
    private val data = listOf("苹果", "香蕉", "橘子", "香梨", "菠萝", "葡萄")
    private val fruitList = ArrayList<Fruit>()

    /*广播部分*/
    lateinit var timeChangeReceiver: TimeChangeReceiver

    /*接收器*/
inner class TimeChangeReceiver:BroadcastReceiver(){
    override fun onReceive(context: Context?, intent: Intent?) {
        Toast.makeText(context,"Time has changed!",Toast.LENGTH_SHORT).show()
    }

}
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_first)
        initFruits()
        //列表
        val adapter = FruitAdapter(this, R.layout.item_fruit, fruitList)
        //val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, data)
        list_view.adapter = adapter
        //list_view.setOnItemClickListener{parent,view,position,id-> Kotlin中允许将未用到的参数用_代替
        list_view.setOnItemClickListener { _, _, position, _ ->
            val fruit = fruitList[position]
            Toast.makeText(this, fruit.name, Toast.LENGTH_SHORT).show()
        }

        //   val button1: Button=findViewById(R.id.btn1)
        //按钮1事件
        btn1.setOnClickListener {
            Toast.makeText(this, "delicious", Toast.LENGTH_SHORT).show()
        }
        //按钮2事件
        btn2.setOnClickListener {
            // val intent= Intent(this,SecondActivity::class.java)
            val intent = Intent("com.tiany.kotlin_study.ACTION_START")
            intent.addCategory("com.tiany.kotlin_study.MY_CATEGORY")
            startActivity(intent)
        }
        //按钮3
        btn3.setOnClickListener {
            val intent = Intent(this, FiveActivity::class.java)
            startActivity(intent)
        }
        //按钮4
        btn4.setOnClickListener {
            val intent=Intent("com.tiany.kotlin_study.MY_BROADCAST")
            intent.setPackage(packageName)
            sendOrderedBroadcast(intent,null)
        }

        //广播部分
        val intentFilter=IntentFilter()
        intentFilter.addAction("android.intent.action.TIME_TICK")
        timeChangeReceiver=TimeChangeReceiver()
        registerReceiver(timeChangeReceiver,intentFilter)

    }

    /*回调结束后注销广播接收器*/
    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(timeChangeReceiver)
    }

    /*初始化菜单*/
    private fun initFruits() {
        repeat(50) {//填充两次
            fruitList.add(Fruit("苹果", R.drawable.apple))
            fruitList.add(Fruit("香蕉", R.drawable.banana))
            fruitList.add(Fruit("橘子", R.drawable.orange))
            fruitList.add(Fruit("葡萄", R.drawable.grapes))
            fruitList.add(Fruit("菠萝", R.drawable.pineapple))
            fruitList.add(Fruit("香梨", R.drawable.pear))
        }
    }

    //菜单事件
    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.main, menu)
        return true
    }

    //菜单点击（选择）事件
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.add_item -> Toast.makeText(this, "add", Toast.LENGTH_SHORT).show()
            R.id.remove_item -> Toast.makeText(this, "remove", Toast.LENGTH_SHORT).show()
            R.id.finish_item -> finish()
        }
        return true
    }
}